﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace AppEnvironment.Web
{
    /// <summary>
    /// Groups LoadOnceOnlyControl's on a page according to Group, ensures only the first one renders.
    /// Use to load javascript reference from multiple UserControls/Masterpage - ensure only one reference gets loaded.
    /// </summary>
    public class LoadOnceOnlyControl: PlaceHolder
    {
        /// <summary>
        /// Groups LoadOnceOnlyControl's on a page according to Group, ensures only the first one renders
        /// </summary>
        public string Group { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            base.OnInit(e);
            Visible = false;

            if (string.IsNullOrEmpty(Group))
            {
                throw new Exception("Group property is not set");
            }

            object isLoadedObj = App.ContextItems[Group];
            if ((isLoadedObj != null) && ((bool)isLoadedObj))
            {
                Visible = false;
            }
            else
            {
                App.ContextItems[Group] = true;
                Visible = true;
            }
        }
    }
}
